package com.it.selfAfter.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.it.selfAfter.dto.ProductionDTO;
import com.it.selfAfter.entity.Production;
import com.it.selfAfter.entity.ProductionType;
import lombok.Data;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface ProductMapper extends BaseMapper<Production> {

    @Select("select * from production_type where p_type = 'p_type'")
    List<ProductionType> getProInfo ();

    @Insert("insert into production(create_time, phone, name, create_user, area, address,production_type,user_id,description,imgs) " +
            "values (#{createTime},#{phone},#{name},#{createName},#{result},#{address},#{productionType},#{userId},#{description}," +
            "#{keys})")
    int saveProduct (@Param("name") String name, @Param("productionType") String productionType, @Param("result") String result,
                     @Param("address") String address, @Param("description") String description, @Param("phone") String phone,
                     @Param("userId")  String userId, @Param("createName") String createName, @Param("keys") String keys,
                     @Param("createTime") Date createTime);

    @Select("SELECT p.*, u.images\n" +
            "FROM production p left join sys_users u on p.user_id = u.user_id\n" +
            "WHERE DATE(p.create_time) BETWEEN IFNULL(#{min_date}, DATE(p.create_time)) AND IFNULL(#{max_date}, DATE(p.create_time))\n" +
            "  AND IFNULL(p.production_type, '') = IFNULL(#{pType}, p.production_type)\n" +
            "  AND IFNULL(p.area, '') = IFNULL(#{area}, p.area)\n" +
            "  AND (IFNULL(p.name, '') LIKE CONCAT('%', #{name}, '%'))\n" +
            "ORDER BY p.create_time DESC \n" +
            "LIMIT #{startIndex}, #{size}")
    List<ProductionDTO> queryProductList (@Param("min_date") String min_date, @Param("max_date") String max_date, @Param("area") String area, @Param("pType")  String pType,
                                          @Param("name") String name, @Param("size") Integer size, @Param("startIndex")  Integer startIndex);

    @Select("select p.*, u.images AS headimgurl, u.name AS user_name, u.username AS nick_name, c.user_id AS collect\n" +
            "from production p\n" +
            "         left join sys_users u on p.user_id = u.user_id\n" +
            "         left join production_collect c on #{userId} = c.user_id and p.id = c.pro_id\n" +
            "where p.id = #{id}")
    Map<String, Object> getProductInfoById(@Param("id") Long id, @Param("userId") String userId);

    @Select("select p_value from production_type where p_type = 'p_type'")
    List<String> getProductBaseInfo();

    @Select("select * from production where id = #{id}")
    Production getProductById(@Param("id") long id);

    @Delete("DELETE FROM production_collect where user_id =  #{userId} and pro_id = #{proId}")
    int updateCollectById(@Param("proId") String proId, @Param("userId") String userId);

    @Insert("insert into production_collect(phone, pro_id, user_id) values (#{phone}, #{proId}, #{userId})")
    int saveCollectById(@Param("phone") String phone, @Param("proId") String proId, @Param("userId") String userId);

    @Select("select * from production where id = #{proId}")
    ProductionDTO echoProductInfoById(@Param("proId") String proId);
}
